package com.lichuandi.crawler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/movies")
public class MovieController {

    @GetMapping("/top")
    public String getTopMovies() {
        try {
            String url = "http://example.com/movies"; // 要爬取的电影网站URL
            Document document = Jsoup.connect(url).get();

            Elements movies = document.select(".movie"); // 获取电影排行榜的元素
            StringBuilder result = new StringBuilder();

            for (Element movie : movies) {
                String rank = movie.select(".rank").text(); // 获取排名
                String title = movie.select(".title").text(); // 获取电影标题
                String rating = movie.select(".rating").text(); // 获取评分
                String description = movie.select(".description").text(); // 获取简介

                result.append(rank).append(". ").append(title).append(", Rating: ").append(rating).append("\n")
                        .append("Description: ").append(description).append("\n\n");
            }

            return result.toString();
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }
}
